<?php
/**
 * Created by PhpStorm.
 * User: liubo
 * Date: 2018/3/12
 * Time: 10:01
 */
namespace app\back\controller;

use think\Controller;
use app\back\model\%model%;
use app\back\validate\%validate%;
use think\Db;
use think\Session;
class %controller% extends Controller
{

    //修改操作
    public function SetAction(){
        $id= input('get.id');//获取id
        $this->assign('id',$id);

        $request=request();//助手函数
        if($request->isGet()){
            if(Session::get('message')==''&&Session::get('data')==''){
                $message = [];
                $data = [];

                //如果不为空 插入数据库 显示出来
                if(!empty($id))
                    $data=Db::name('%table%')->find($id);//id主键 查询出来的数据
            }
            else{
                $message  = Session::get('message');
                $data = Session::get('data');
            }

            $this->assign('message',$message);
            $this->assign('data',$data);
            return $this->fetch();


        }
        elseif($request->isPost()){
            //数据库入库 获取数据
            $post_result=input('post.');
            //验证实例化
            $brand_validate = new %validate%;
            //check就是验证用户输入的数据是否合理
            //batch(批量验证)
            if( !$brand_validate->batch(true)->check($post_result)){
                return $this->redirect('set',[],302,[
                    'message'=>$brand_validate->getError(),
                    'data'=>$post_result
                ]);
            }else{

                //实例化
                $result=new %model%;//如何修改数据
                if(!empty($post_result['id'])){//
//                echo 1;
                    $m=$result->find($post_result['id']);
                    $m->save($post_result);
                }
                else{
//                dump($post_result);
                    $result->save($post_result);//插入数据
                }

                if($request){
                    return $this->redirect('Index');
                }
                else{
                    return $this->redirect('set');
                }
            }

        }
        else{

        }
//    return $this->fetch();
    }
    //查询操作
    public  function  indexAction(){
        $model=new %model%;
        //实例化
        //筛选
        //接受前台传递过来数组的参数
        $filter=input('filter/a',[]);
        $filter_order=[];

    %where_list%


        //排序  先声明
        $order=input('order/a');
        if(!empty($order)){
            $model->order([$order['field']=>$order['type']]);
        }

        //分页
        $limt=2;
        $list=$model->paginate($limt,false,['query' => request()->param()]);
        $start=$limt*($list->currentPage()-1)+1;
        $end=$start+count($list)-1;

        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('filter',$filter);
        $this->assign('filter_order',$filter_order);
        $this->assign('order',$order);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function multiAction(){
        $selected=input('selected/a',[]);
        if(empty($selected)){
            return $this->redirect('index');
        }
        //批量删除
    %model%::destroy($selected);
        return $this->redirect('index');
    }
}

//http://www.shop.com/index.php/back/Brand/Brand